﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace NMF.Languages
{
    public abstract class Property
    {
        public abstract Type Type { get; }

        public abstract void SetAPrioriType(Type type);

        public abstract string Name { get; }

        public abstract object GetValue(object source);

        public abstract void SetValue(object source, object value);

        public abstract Class DeclaringClass { get; }

        public abstract bool IsCollectionProperty { get; }

        public abstract void AssertCollectionProperty(Type collectionType);

        public abstract void AssertProperty(Type propertyType);

        public abstract void AssertIsValidFor(Class declaringClass);

        public virtual void InitializeDefaultValue(object source)
        {
        }

        public virtual IEnumerable GetItems(object source)
        {
            return GetValue(source) as IEnumerable;
        }

        public virtual void AddItem(object source, object value)
        {
            if (IsCollectionProperty)
            {
                var list = GetValue(source) as IList;
                if (list != null)
                {
                    list.Add(value);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                throw new InvalidOperationException("no collection property");
            }
        }

        public virtual void RemoveItem(object source, object value)
        {
            if (IsCollectionProperty)
            {
                var list = GetValue(source) as IList;
                if (list != null)
                {
                    list.Remove(null);
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
    }
}
